edit_submit:
;in ->  eax = coords y shl 16 + x
; only submit !!
;       esi - src
;       edi - dst

    .mx                    equ ebp-36
    .points_src            equ ebp-40
    .points_rotated_dst    equ ebp-44
    .points                equ ebp-60

        push     ebp
        mov      ebp,esp
        sub      esp,64
        ; store both x and y coordinates
        mov      [.points_src],esi
        mov      [.points_rotated_dst],edi
        mov      esi,matrix_scaled
        lea      edi,[.mx]
        call     reverse_mx_3x3
      .edit_submit:
        ; submit both causes long pipe and vertices  edition
        mov      esi,dword[edit_particle_no]
        mov      edi,esi
        dec      esi
        mov      edi,esi
        imul     esi,12
        add      esi,[.points_rotated_dst]
        movups   xmm0,[esi]
        movups   xmm7,[xxadd]
        cvtdq2ps xmm7,xmm7
        subps    xmm0,xmm7
        movups   [.points],xmm0
        lea      esi,[.points]
        imul     edi,12
        add      edi,[.points_src]
        lea      ebx,[.mx]
        mov      ecx,1
        call     rotary
   .end:
        mov      esp,ebp
        pop      ebp
ret
;=================================================================
write_edit_bars:
    push     ebp
    mov      ebp,esp

    .counter       equ ebp-4
    .x_res4m24     equ ebp-8
    .xresm4        equ ebp-12
    .yresm4        equ ebp-16
    .tri_counter   equ ebp-20
    .ed_counter    equ ebp-24
    .cnst6         equ ebp-28
    .scr           equ ebp-32
    .edit          equ ebp-36
    .xres          equ dword [ebp-40]
    .points_rot    equ dword [ebp-44]
    .i33           equ ebp-60

    xor      edx,edx
    push     edx
    movzx    eax,word[xres_var]
    mov      ebx,eax
    shl      eax,2
    sub      eax,24
    push     eax        ;  .x_res4m24
    movzx    eax,word[yres_var]
    sub      eax,4
    sub      ebx,4
    push     ebx        ; .xresm4
    push     eax        ; .yresm4
    xor      edx,edx

    push     edx       ; init counters
    push     edx
    mov      edi,[screen_ptr]
    mov      ebx,[edit_buf_ptr]
    movzx    edx,[xres_var]
    push     dword 6
    push     edi
    push     ebx
    push     edx
    push     [points_rotated_ptr]
    sub      esp,16
    mov      eax,0.33333333
    movd     xmm6,eax
    shufps   xmm6,xmm6,0
    movups   [.i33],xmm6

    mov       al,[edit_flag]
    cmp      al,2
    je       .edit_face
    cmp      al,3
    je       .edit_edge
    mov      esi,.points_rot
    cld
  .loop:                ; edit vertex
    push      esi
    cmp       [culling_flag],1
    je        @f
    mov       ebx,[.counter]
    imul      ebx,12
    add       ebx,[points_normals_rotated_ptr]
    bt        dword[ebx+8],31
    jnc       .skip
  @@:
 ;  ecx - edge index; signature
 ;  xmm0 - coordinates  , as floats
 ;  eax - color
 ;  edx  - xresm4
 ;  esi  - yresm4
    pushad
    movups   xmm0,[esi]
    mov      eax,0x00ffff00
    mov      edx,[.xresm4]
    mov      esi,[.yresm4]
    mov      ecx,dword[.counter]
    call     bar_edge
    popad
  .skip:
    pop      esi
    add      esi,12
    inc      dword[.counter]
    mov      ecx,[.counter]
    cmp      ecx,[points_count_var]
    jng      .loop
    jmp      .end


 .edit_face:
    mov      esi,[triangles_ptr]
    xor      ecx,ecx
  .loop_face:
    push     ecx
    lodsd
    mov      edx,.points_rot
    imul     eax,12
    movups   xmm0,[eax+edx]
    lodsd
    imul     eax,12
    movups   xmm1,[eax+edx]
    lodsd
    imul     eax,12
    movups   xmm2,[eax+edx]
    movups   xmm6,[.i33]
    addps    xmm0,xmm1
    addps    xmm0,xmm2
    mulps    xmm0,xmm6
    pushad
    mov      eax,0x00ffff00
    mov      edx,[.xresm4]
    mov      esi,[.yresm4]
    call     bar_edge
    popad
  .skip_face:
    pop      ecx
    inc      ecx
    cmp      ecx,[triangles_count_var]
    jnz     .loop_face
    jmp     .end

  .edit_edge:

    mov     esi,[edges_ptr]  ; each edge 2 dwords
    mov     ecx,[edges_count]
  .loop_edge:
    push    ecx
    lodsd
    imul    eax,12
    add     eax,.points_rot
    movups  xmm0,[eax]
    lodsd
    imul    eax,12
    push    esi
    add     eax,.points_rot
    movups  xmm1,[eax]
    addps   xmm0,xmm1
    mulps   xmm0,[f05x3];
    mov     ecx,[.ed_counter]
    mov     edx,[.xresm4]
    mov     esi,[.yresm4]
    mov     eax,0x00ffff00
    call    bar_edge
    pop     esi
    pop     ecx
    inc     dword [.ed_counter]
    loop    .loop_edge

  .end:
    mov     esp,ebp
    pop     ebp
ret
;==================================================================
bar_edge:
 ;  ecx - edge index; signature
 ;  xmm0 - coordinates  , as floats
 ;  eax - color
 ;  edx  - xresm4
 ;  esi  - yresm4
    push     ebp
    mov      ebp,esp

    .xresm4   equ ebp-4
    .yresm4   equ ebp-8
    .ed       equ ebp-12
    .col      equ ebp-16
    .add      equ ebp-20


    push     edx
    push     esi
    push     ecx
    push     eax
    movzx    eax,[xres_var]
    sub      eax,6
    shl      eax,2
    push     eax

    cvtps2dq xmm0,xmm0
    mov      eax,5
    movaps   xmm1,xmm0
    movaps   xmm2,xmm0
    movups   xmm3,[.yresm4]
    movd     xmm4,eax
    shufps   xmm3,xmm3,11100001b
    shufps   xmm4,xmm4,0
    pcmpgtd  xmm1,xmm3
    pcmpgtd  xmm2,xmm4
    xorps     xmm1,xmm2
    pmovmskb eax,xmm1
    cmp      al,-1
    jne      .skip

    sub      esp,8
    movlps   [esp],xmm0
    pop      eax ebx

    sub      eax,3
    sub      ebx,3
    xchg     eax,ebx
    movzx    edx,[xres_var]
    mul      edx
    add      ebx,eax
    shl      ebx,2
    mov      edi,ebx
    add      edi,[screen_ptr]
    add      ebx,[edit_buf_ptr]
    mov      edx,dword[.ed]
    cld
  ;  draw bar 6x6
    mov      ecx,6
;    movss    xmm1,[.col]
;    movd     xmm2,edx
;    shufps   xmm1,xmm1,0
;    shufps   xmm2,xmm2,0
;    movups   [esi],xmm1
;    movups   [ebx],xmm2
;    movlps   [esi+16],xmm1
;    movlps   [ebx+16],xmm2
;    int3
    mov      eax,[.col]
 .oop_ed:
    push     ecx
    mov      ecx,6
  @@:
    stosd
    mov      [ebx],edx
    add      ebx,4
    loop     @b

    add      edi,[.add]
    add      ebx,[.add]
    pop      ecx
    loop     .oop_ed

  .skip:
    mov      esp,ebp
    pop      ebp
ret

;===============================================================
write_edit_bars_vert:
; this proc used for vertices edit module and for closed
; series Bezier curves edit module (many "sticked" pipes).
;in:
;   eax - x resolution of image
;   ebx - y resolution of image
;   esi - ptr vertices to display
;   ecx - count vertices to display
;   edi - ptr to edit buffer
;   edx - ptr to screen buffer normal
;   xmm0 = 1 - bezier patch bars cause, otherwise - normal vert
;   xmm0 = mark
;  .mark = 'add' add value in two foats in low half xmm1
;           index in xmm3



    push      ebp
    mov       ebp,esp

    .yres        equ ebp-4
    .xres        equ ebp-8
    .vert_ptr    equ ebp-12
    .vert_count  equ ebp-16
    .edit_b_ptr  equ ebp-20
    .scr_ptr     equ ebp-24
    .x_res4m24   equ ebp-28
    .yresm4      equ ebp-32
    .xresm4      equ ebp-36
;    .mark        equ [ebp-40]


    push     ebx
    push     eax
    push     esi
    push     ecx
    push     edi
    push     edx
    shl      eax,2
    sub      eax,24
    push     eax
    mov      eax,[.xres]
    sub      eax,4
    sub      ebx,4
    push     ebx
    push     eax
;    movss    .mark,xmm0
    mov      esi,[.vert_ptr]
    xor      ecx,ecx
  .loopA:
    push     ecx
    push     esi
    movups   xmm0,[esi]
;  ecx - edge index; signature
;  xmm0 - coordinates  , as floats
;  eax - color
;  edx  - xresm4
;  esi  - yresm4
    pushad
    mov      eax,0x00ffff00
    mov      esi,[.yresm4]
    mov      edi,[.xresm4]
    call     bar_edge
    popad
  .skip:
    pop      esi
    add      esi,12
    pop      ecx
    inc      ecx
    cmp      ecx,[.vert_count]
    jng      .loopA
    mov      esp,ebp
    pop      ebp
ret